﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.TownHall.ServiceClient;

namespace NASA.BeAMartian.Views.Learn
{
    public partial class TopicListPage : PhoneApplicationPage
    {
        object _selectedItem;
        public THServiceClient client = new THServiceClient();
        private TopicList Topics = new TopicList();

        public TopicListPage()
        {
            InitializeComponent();
            client.TopicsDownloaded += new TopicsDownloadedEventHandler(client_TopicsDownloaded);

            FrameworkElement _root = Application.Current.RootVisual as FrameworkElement;
            ClientState _clientState = (ClientState)_root.DataContext;
            string categorySlug = String.Empty;

            Category c = null;
            if (_clientState != null && _clientState.CurrentCategory != null)
            {
                c = _clientState.CurrentCategory;
                textBlockPageTitle.Text = String.Format("{0} - Topics", c.Name);
            }

            //TODO: Add check for the need to refresh 
            client.TopicsDownloaded += new TopicsDownloadedEventHandler(client_TopicsDownloaded);
            if (c!= null && _clientState != null && _clientState.CategoryTopicCache != null && _clientState.CategoryTopicCache.ContainsKey(c.Name))
            {
                Topics = _clientState.CategoryTopicCache[c.Name].Topics;

                Dispatcher.BeginInvoke(delegate()
                {
                    lbTopics.ItemsSource = Topics;
                    TopicCache tc = new TopicCache();
                    tc.LastUpdated = DateTime.UtcNow;
                    tc.Topics = Topics;
                    _clientState.CategoryTopicCache.Add(categorySlug, tc);

                });
            }
            else
            {

                if (c != null)
                {
                    if (c.Slug != null)
                    {
                        categorySlug = c.Slug;
                    }
                }



                if (String.IsNullOrEmpty(categorySlug))
                    MessageBox.Show("empty topic");
                else
                    client.GetTopicsForCategory(categorySlug);
            }

        }

        private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            FrameworkElement _root = Application.Current.RootVisual as FrameworkElement;
            ClientState clientState = (ClientState)_root.DataContext;
            if (clientState == null) { clientState = new ClientState(); }
            clientState.CurrentTopic = lbTopics.SelectedItem as Topic;
            _root.DataContext = clientState;

            this.NavigationService.Navigate(new Uri("/Views/Learn/QuestionListPage.xaml", UriKind.Relative));
        }

        void client_TopicsDownloaded(object sender, TopicsDownloadedEventArgs e)
        {
            Topics = e.Topics;

            Dispatcher.BeginInvoke(delegate()
            {
                lbTopics.ItemsSource = Topics;
            });
        }

        #region appBarNavigation
        private void appbarCategories_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/CategoryListPage.xaml", UriKind.Relative));

        }
        private void appbarProfile_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/UserProfilePage.xaml", UriKind.Relative));
        }
        private void appbarSearch_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/MoreListPage.xaml", UriKind.Relative));

        }
        private void appbarMore_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/MoreListPage.xaml", UriKind.Relative));

        }
        #endregion

        private void ItemText_Loaded(object sender, RoutedEventArgs e)
        {
            if ((sender as TextBlock).Text.ToLower().Trim() == "spirit postcards")
                Utils.VisualTreeUtils.FindParentByType<ListBoxItem>(sender as FrameworkElement).Visibility = System.Windows.Visibility.Collapsed;
        }
    }
}